package com.example.smartg;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.smartg.utils.LogoutDialogUtil;
import com.example.smartg.utils.SuperUserDialogUtil;
import com.example.smartg.utils.ToastUtil;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.security.auth.callback.Callback;

public class ManagerLoginActivity extends AppCompatActivity {

    //加上计时器
    private Timer mTimer; // 计时器，每2秒执行一次任务
    private MyTimerTask mTimerTask; // 计时任务，判断是否未操作时间到达ns

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager_login);
        // #start 管理员通用模块
        TextView textViewUsername = (TextView) findViewById(R.id.user_name);
        String user_name = SpUtils.getString(this,"user_name","未登录");
        textViewUsername.setText(user_name);
        textViewUsername.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogoutDialogUtil.logout(ManagerLoginActivity.this);
            }
        });
        ImageView home_logo = (ImageView) findViewById(R.id.home_logo);
        home_logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        // #end 管理员通用模块

        Button btn_ge_manage = (Button) findViewById(R.id.btn_ge_manage);
        btn_ge_manage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ManagerLoginActivity.this,GemanageActivity.class);
                startActivity(intent);
            }
        });
        Button btn_fingerprint_add = (Button) findViewById(R.id.finger_print_add);
        btn_fingerprint_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.success(ManagerLoginActivity.this, "开发中。。。");
            }
        });
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.success(ManagerLoginActivity.this, "开发中。。。");
            }
        };
        Button btn_user_register = (Button) findViewById(R.id.user_register);
        btn_user_register.setOnClickListener(listener);
        Button btn_auth_manage_btn = (Button) findViewById(R.id.auth_manage_btn);
        btn_auth_manage_btn.setOnClickListener(listener);
        Button btn_config_manage = (Button) findViewById(R.id.config_manage);
        btn_config_manage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SuperUserDialogUtil.auth(ManagerLoginActivity.this);
            }
        });
        Button btn_software_update = (Button) findViewById(R.id.software_update);
        btn_software_update.setOnClickListener(listener);

        // 加上计时器
        startTimer();
    }


    @Override
    protected void onPause() {
        stopTimer();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopTimer();
    }
    // 记录上一次操作时间
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        SpUtils.putLong(this,"lastActionTime",System.currentTimeMillis());
        return super.dispatchTouchEvent(ev);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public void exitAPP() {
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.AppTask> appTaskList = activityManager.getAppTasks();
        for (ActivityManager.AppTask appTask : appTaskList) {
            appTask.finishAndRemoveTask();
        }
    }

    private class MyTimerTask extends TimerTask {

        @Override
        public void run() {
            //Log.e(TAG, "计时中……");
            // 1分钟未操作停止计时并退出登录
            TextView tv = (TextView) findViewById(R.id.countdown3);
            long mLastActionTime = SpUtils.getLong(ManagerLoginActivity.this,"lastActionTime",0);
            if((System.currentTimeMillis() - mLastActionTime - 5000) / 1000 > 0){
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tv.setText( 60 - (System.currentTimeMillis() - mLastActionTime - 5000) / 1000 + "秒后退出！！");
                    }
                });
            }else{
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tv.setText("");
                    }
                });
            }
            Log.e("=1=", (System.currentTimeMillis() - mLastActionTime - 5000) / 1000 + "");
            if (System.currentTimeMillis() - mLastActionTime - 5000 > 1000 * 60 * 1) {
                stopTimer();
                SpUtils.clear(ManagerLoginActivity.this);
                finish();
            }

        }
    }

    public void stopTimer() {
        Log.e("=1=", "主页定时任务关闭");
        mTimer.cancel();
    }

    private void startTimer() {
        Log.e("=1=", "主页定时任务启动");
        mTimer = new Timer();
        mTimerTask = new MyTimerTask();
        // 初始化上次操作时间为登录成功的时间
        SpUtils.putLong(this,"lastActionTime",System.currentTimeMillis());
        // 每过1s检查一次
        mTimer.schedule(mTimerTask, 0, 1000);
        //Log.e(TAG, "登录成功开始计时");

    }
}